cmake_minimum_required(VERSION 3.20)

set(qqgame-cpp_VERSION 1.0.0)
project(qqgame-cpp
	VERSION ${qqgame-cpp_VERSION}
	DESCRIPTION "QQ 游戏大厅，websocket 通信协议的 C++ 实现"
	HOMEPAGE_URL "https://gitee.com/zyf/qqgame-cpp"
	LANGUAGES CXX)

include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

# 构建 lib 库的规则
file(GLOB LIB_SRCS src/*.cpp)
add_library(qqgame-cpp ${LIB_SRCS})
target_compile_features(qqgame-cpp PUBLIC cxx_std_17)
find_package(nlohmann_json CONFIG REQUIRED)
target_link_libraries(qqgame-cpp PRIVATE nlohmann_json::nlohmann_json)
target_include_directories(qqgame-cpp
    PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
           $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

if (BUILD_SHARED_LIBS)
	set_property(TARGET qqgame-cpp PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
else()
	set_property(TARGET qqgame-cpp PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

# cmake config 配置
configure_package_config_file(cmake/config.cmake.in
	${CMAKE_CURRENT_BINARY_DIR}/qqgame-cpp-config.cmake
	INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/qqgame-cpp
	NO_SET_AND_CHECK_MACRO)
write_basic_package_version_file(
	${CMAKE_CURRENT_BINARY_DIR}/qqgame-cpp-config-version.cmake
	VERSION ${qqgame-cpp_VERSION}
	COMPATIBILITY SameMajorVersion)
# 安装规则
install(
	FILES ${CMAKE_CURRENT_BINARY_DIR}/qqgame-cpp-config.cmake
          ${CMAKE_CURRENT_BINARY_DIR}/qqgame-cpp-config-version.cmake
	DESTINATION ${CMAKE_INSTALL_DATADIR}/qqgame-cpp)

install(TARGETS qqgame-cpp EXPORT qqgame-cpp-targets)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/qqgame-cpp)
install(EXPORT qqgame-cpp-targets
	NAMESPACE qqgame-cpp::
	DESTINATION ${CMAKE_INSTALL_DATADIR}/qqgame-cpp)

# 构建测试程序
if (qqgame-cpp_BUILD_TEST)
	add_executable(test_qqgame-cpp test/test.cpp)
	target_link_libraries(test_qqgame-cpp PRIVATE qqgame-cpp)
	if (BUILD_SHARED_LIBS)
		set_property(TARGET test_qqgame-cpp PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
	else()
		set_property(TARGET test_qqgame-cpp PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
	endif()
	# 拷贝测试附加参数
	add_custom_command(TARGET test_qqgame-cpp POST_BUILD
	COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_LIST_DIR}/test/extra_qqgame_test_params.txt $<TARGET_FILE_DIR:test_qqgame-cpp>)
endif()

